package com.example.demo.suanfa_jiegou;

import java.util.Stack;

/**
 * 利用stack结构，返回 一组数内最小值
 * 使用两个栈结构，pop返回当前结构的最小值。
 *
 */
public class MInValueStack {
    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    public MInValueStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int value) {
        stack.push(value);

        if (minStack.isEmpty() ){
            minStack.push(value);
            return;
        }
        if (minStack.peek() > value) {
            minStack.push(value);
        } else {
            minStack.push(minStack.peek());
        }
    }

    public int pop(){
        minStack.pop();
        return stack.pop();
    }

    public int min(){
        return minStack.peek();
    }

    public static void main(String[] args) {
        MInValueStack stack = new MInValueStack();
        stack.push(12);
        stack.push(512);
        stack.push(412);
        stack.push(122);
        stack.push(2);
        stack.push(1);
        stack.push(112);

        System.out.println(stack.min());
    }

}
